var server = require('api');
var _alert = require('alert');
var login = avalon.define({
	$id : 'login',
    display : true,
	user : {
        username : '',
        password : ''
    },
    test: function () {
      return true;
    },
    errorMessage : '',
    successInputName : [],
    errorInputName : [],
    loginValidate : {
        onSuccess : function (reasons){
            if (login.successInputName.indexOf(this.id) === -1) login.successInputName.push(this.id.toString());
            if (login.errorInputName.indexOf(this.id) > -1) login.errorInputName.splice(login.errorInputName.indexOf(this.id),1);
        },
        onError: function (reasons) {
            login.errorMessage = reasons[0].getMessage();

            if (login.successInputName.indexOf(this.id) > -1) login.successInputName.splice(login.successInputName.indexOf(this.id),1);
            if (login.errorInputName.indexOf(this.id.toString()) === -1) login.errorInputName.push(this.id.toString());

        }
    },
	bgimg : require('../../img/login/login-bg.jpg'),
    title : require('../../img/login/title.png'),
	admin: require('../../img/login/admin.png'),
	pwd: require('../../img/login/password.png'),

	init: function () {
        avalon.log("login init"+_.now());
        setTimeout(function () {
            //解决IE下CORS无效，ajax失败的问题
            $.support.cors = true;
        },100)
    },
    login: function(e){

	    e.preventDefault();

        if (login.errorInputName.length > 0){
            return false;
        }else {
            $.ajax({
                type:'get',
                url:server.login,
                data:{userAccount:login.user.username,userPassword:login.user.password},
                dataType:'json'
            })
                .done(function (res) {
                    var data = res.values;
                    if (res.status == '9200'){
                        avalon.vmodels.root.needLogin = !avalon.vmodels.root.needLogin;
                        avalon.vmodels.root.user.id = data[0].userId;
                        avalon.vmodels.root.user.token = data[0].userToken;
                        avalon.vmodels.root.user.userType = data[0].userType;
                        avalon.vmodels.root.user.groupId = data[0].ownId;
                        avalon.vmodels.root.user.userName = data[0].userName;
                        avalon.vmodels.root.user.menuList = avalon.mix(true, [],data[0].authChair);


                        //这里需要根据不同角色默认进入不同的模块
                        setTimeout(function () {

                            if (data[0].userType == 'total'){
                                avalon.router.navigate("userTable/userTable", 2);
                            } else if (data[0].userType == 'totaldep'){
                                avalon.router.navigate("task/task", 2);
                            } else if (data[0].userType == 'branch'){
                                avalon.router.navigate("userTable/userTable", 2);
                            } else if (data[0].userType == 'branchdep'){
                                avalon.router.navigate("uploadTask/uploadTask", 2);
                            }
                        },10)
                    }else{
                        _alert('登录失败: ' + res.description);
                    }
                })
                .fail(function (res) {
                    _alert('登录失败：网络错误，请刷新后重试！');
                })
        }

    }
})

module.exports = login;